/**
 * Smart City use case October 2012 
 */
package com.orange.smartcity.android;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import static com.orange.smartcity.android.SmartCityCommon.*;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;

/**
 * @author FAIROUZ OUBENALI
 *
 */
public class CEPEventReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Log.i(CEP_EVENT_RECEIVER_TAG,
				" ************************************************"
						+ "\n Received message GCM:: "
						+ intent.getStringExtra("data"));
		JSONObject data;
		// final String action = intent.getAction();
		String cloudTypeName;
		String message;
		String latitude;
		String longitude;
		try {
			data = (JSONObject) new JSONTokener(intent.getStringExtra("data"))
					.nextValue();
			Log.i(CEP_EVENT_RECEIVER_TAG,
					" ************************************************"
							+ "\n Received message GCM:: " + data.toString());
			cloudTypeName = data.getString("cloudTypeName");
			message = data.getString("messag");
			
			int icon = 0;
			if (cloudTypeName.equals("TRANSPORT"))
				icon = R.drawable.transport;
			else
			{
				icon = R.drawable.shopping;
				latitude = data.getString("latitude");
				longitude = data.getString("longitude");
			}
			if(isOnline==true)
			notification(context, message, cloudTypeName, icon);
			else
				Log.i(CEP_EVENT_RECEIVER_TAG,
						" ************************************************"
								+ "\n offline:: " +isOnline);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void onReceivePhoneStateChanged(final Context context,
			final Intent intent) {
		final String phoneState = intent
				.getStringExtra(TelephonyManager.EXTRA_STATE);
		final String phoneNumber = intent
				.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
		// event.eventType = EVENT_TYPE_PHONE_STATE_CHANGED;
		final StringBuilder sb = new StringBuilder();
		if (TelephonyManager.EXTRA_STATE_IDLE.equals(phoneState))
			sb.append("idle");
		else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(phoneState))
			sb.append("offhook");
		else if (TelephonyManager.EXTRA_STATE_RINGING.equals(phoneState))
			sb.append("ringing");
		else
			sb.append(phoneState);
		if (phoneNumber != null) {
			sb.append(',');
			sb.append("number:" + phoneNumber);
		}
		// event.params = sb.toString();
		// service.acceptIncomingEvent(event);
	}

}
